package com.godpaper.chessGameHall.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.godpaper.chessGameHall.busniess.delegates.ConfigDelegate;
	import com.godpaper.chessGameHall.events.TryConnectServerEvent;
	import com.godpaper.chessGameHall.events.TryLoadConfigXmlEvent;
	import com.godpaper.chessGameHall.model.ConfigModelLocator;
	import com.godpaper.chessGameHall.model.ConnectionModelLocator;
	
	import mx.rpc.IResponder;
	
	public class TryLoadConfigXmlCommand extends CommandBase implements ICommand, IResponder
	{
		private var delegate:ConfigDelegate;
		private var tryLoadConfigXmlEvent:TryLoadConfigXmlEvent;
		private var configModel:ConfigModelLocator = ConfigModelLocator.getInstance();
		private var connectionModel:ConnectionModelLocator = ConnectionModelLocator.getInstance();
		private var tryConnectServerEvent:TryConnectServerEvent;
		
		public function TryLoadConfigXmlCommand()
		{
			super();
			delegate = new ConfigDelegate(this);
		}
		
		override public function execute(event:CairngormEvent):void
		{
			tryLoadConfigXmlEvent = TryLoadConfigXmlEvent(event);
			delegate.load(null);
		}
		
		override public function result(data:Object):void
		{
			//TODO: implement function
			configModel.sfsConfigVO.defaultRoom = data.result.SmartFoxServerConfig.defaultRoom;
			configModel.sfsConfigVO.ip 			= data.result.SmartFoxServerConfig.ip;
			configModel.sfsConfigVO.port 		= data.result.SmartFoxServerConfig.port;
			configModel.sfsConfigVO.xtName 		= data.result.SmartFoxServerConfig.xtName;
			configModel.sfsConfigVO.xtScript 	= data.result.SmartFoxServerConfig.xtScript;
			configModel.sfsConfigVO.zone 		= data.result.SmartFoxServerConfig.zone;
			trace("configModel.sfsConfigVO:",configModel.sfsConfigVO);
			
			//Connect to server
			tryConnectServerEvent = new TryConnectServerEvent( configModel.sfsConfigVO.ip,
															   configModel.sfsConfigVO.port );
			
			CairngormEventDispatcher.getInstance().dispatchEvent( tryConnectServerEvent );												   
			
		}
		
		override public function fault(info:Object) : void
		{
			connectionModel.connectionStatus = "Failed loading configuration file";
		}
		
	}
}